<!--
 * @Descripttion: 客户管理列表页
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-02 15:03:05
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-11 20:22:56
 -->
<template>
  <div class="app-container">
    <div class="clearfix">
      <el-card shadow="never" class="oper-btn-card">
        <div class="clearfix">
          <el-form
            class="fl form-no-bottom"
            :inline="true"
            :model="searchData"
            label-position="left"
            label-width="80px"
            @keydown.enter.native="btnSearch"
          >
            <el-form-item>
              <el-input
                v-model="searchData.keywords"
                clearable
                style="width: 360px"
                placeholder="按账户名称,开户银行搜索"
              >
                <el-button slot="append" icon="el-icon-search" type="primary" :loading="btnLoading.search" @click="btnSearch()" />
              </el-input>
            </el-form-item>
          </el-form>
          <el-button class="fr" type="primary" @click="btnAddData">新增账户</el-button>
        </div>
      </el-card>
    </div>
    <vxe-grid
      ref="table"
      class="grid"
      border
      resizable
      highlight-hover-row
      :loading="tableLoading.table"
      :columns="columnsData"
      :data="tableData.table"
      @edit-closed="onEditClosed"
    />
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="pageInfo.rows"
        :page-sizes="[20,30,40]"
        :current-page.sync="pageInfo.page"
        :total="pageInfo.total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />
    </div>
    <el-dialog
      v-if="dialogVisible.save"
      :title="dialogTitle"
      :visible.sync="dialogVisible.save"
      destroy-on-close
      append-to-body
    >
      <Save ref="saveComp" :operation="operation" :data="currentData" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible.save = false">取 消</el-button>
        <el-button type="primary" :loading="btnLoading.sure" @click="btnSureSaveData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { listData, saveData, delData } from '@/api/bsd/account.js'
import { getDelButton, getUpdateButton } from '@/components/Table/oper-btns.js'
import Save from './save'
import DetailView from './detail-view'

export default {
  name: 'AccountList',
  components: { Save },
  mixins: [oftenuse],
  data() {
    return {
      searchData: {},
      dialogTitle: '新增账户',
      dialogVisible: {
        save: false,
        update: false
      },
      optionsData: {},
      currentData: null,
      operation: 'add',
      columnsData: [
        { title: '#', width: 30, type: 'expand',
          slots: {
            content: ({ row, seq }) => {
              const data = {
                props: {
                  data: row
                }
              }
              return [<DetailView {...data}/>]
            }
          }
        },
        { title: '操作', width: 80,
          slots: {
            default: (params) => {
              const { row } = params
              const h = this.$createElement
              const delBtnNode = getDelButton(h, { on: { click: () => { this.btnDelData(row) } }})
              const updateBtnNode = getUpdateButton(h, { props: { type: 'primary' }, on: { click: (e) => { this.btnUpdateData(e, row) } }})
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [updateBtnNode, delBtnNode])
              return [divNode]
            }
          }
        },
        { field: 'name', title: '账户名称', width: 140, editRender: { name: 'ElInput' }},
        { field: 'bankName', title: '开户银行', width: 140, editRender: { name: 'ElInput' }},
        { field: 'remainder', title: '剩余金额', width: 140,
          slots: {
            default: (params) => {
              const { row } = params
              const originAmount = row.originAmount || 0
              const amount = row.amount || 0
              const value = this.$amount(originAmount)
                .add(amount).format()
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'status', title: '状态', width: 80,
          cellRender: {
            name: 'ElSwitch',
            props: { activeValue: '1', inactiveValue: '0' },
            events: {
              change: this.onSwitchStatusChange
            }
          }
        }
      ]
    }
  },
  watch: {
  },
  created() {
    this.getListData()
  },
  methods: {
    btnAddData() {
      this.operation = 'add'
      this.dialogVisible.save = true
    },
    btnSureSaveData() {
      const saveComp = this.$refs.saveComp
      saveComp.valiData().then(_ => {
        const { data } = saveComp.getData()
        this.setBtnLoading('sure', true)
        this.saveData(data, true).then(res => {
          if (res.data.flag) this.$notify.success({ title: '保存成功！' })
          this.setBtnLoading('sure', false)
        })
      })
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.getListData().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    // table btns
    btnUpdateData(e, data) {
      this.operation = 'update'
      this.currentData = data
      this.dialogVisible.save = true
    },
    btnDelData(row) {
      this.$confirm('确定要删除该账户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delListData(row.id)
      }).catch(() => {
      })
    },
    // api
    delListData(id) {
      delData(id).then(res => {
        const message = res.data.message
        if (!res.data.flag) return this.$notify.error({ title: '删除失败!', message: message })
        this.$notify.success({ title: '删除成功!' })
        this.getListData()
      }).catch(err => {
        this.$notify.error({ title: '删除异常！', message: err.message })
      })
    },
    saveData(dataObj, refresh = false) {
      return new Promise((resolve, reject) => {
        saveData(dataObj).then(res => {
          resolve(res)
          const message = res.data.message
          if (!res.data.flag) {
            this.getListData()
            return this.$notify.error({ title: '保存失败!', message: message })
          }
          if (refresh) this.getListData()
          this.dialogVisible.save = false
        }).catch(err => {
          this.$notify.error({ title: '保存异常！', message: err.message })
          reject(err)
        })
      })
    },
    getListData(params = { data: this.searchData, page: this.pageInfo }) {
      this.setTableLoading('table', true)
      return new Promise((resolve, reject) => {
        listData(params).then(res => {
          this.setTableLoading('table', false)
          if (!res.data.flag) return
          const { records, total } = res.data.data
          this.setTableData('table', records)
          this.pageInfo.total = total
          resolve(res)
        }).catch(err => {
          this.setTableLoading('table', false)
          reject(err)
        })
      })
    },
    // on
    onSelectionChange(selectionData) {
      this.setTableSelectionData('table', selectionData)
    },
    onSizeChange(val) {
      this.pageInfo.current = 1
      this.pageInfo.size = val
      this.getListData()
    },
    onEditClosed(params, val) {
      const { row } = params
      this.saveData(row)
    },
    onSwitchStatusChange(params, val) {
      const { row } = params
      this.saveData(row).then(res => {
        if (res.data.flag) {
          this.$set(row, 'status', val)
        }
      })
    },
    onCurrentChange(val) {
      this.pageInfo.current = val
      this.getListData()
    }
  }
}
</script>
<style scoped>

</style>
